<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['technician'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $itemDetailArray     = array();
  $editDetailArray     = array();
	$categoryIdArr       = array();
	$categoryNameArr     = array();
	$subCategoryIdArr    = array();
	$subCategoryNameArr  = array();
	$itemSelected        = array();
	$itemIdArr           = array();
	$itemNameArr         = array();
  $itemName            = "";
  $categoryId          = "";
  $categoryName        = "";
  $subCategoryId       = "";
  $itemMake            = "";
  $subCategoryName     = "";
  $modelNo             = "";
  $currentStock        = "";
  $maxLevel            = "";
  $minLevel            = "";
  $i                   = 0;
  $isEdit              = 0;
  $itemId              = isset($_REQUEST['itemId']) ? $_REQUEST['itemId'] : 0;
  $categorySelected    = (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] != "") ? $_REQUEST['categoryId'] : "";
  $subCategorySelected = (isset($_REQUEST['subCategoryId']) && $_REQUEST['subCategoryId'] != "") ? $_REQUEST['subCategoryId'] : "";
  $itemSelected        = (isset($_REQUEST['subCategoryId']) && $_REQUEST['subCategoryId'] != "") ? $_REQUEST['subCategoryId'] : "";


  if(isset($_POST['cancelBtn']))
	{
    header("Location:item.php");
    exit();
	}
  // after post : start 
  if(isset($_POST['itemName']))
  {
    if($itemId > 0)
    {
    	
      $updateItem = "UPDATE item 
                        SET itemName          = '".$_POST['itemName']."',
                             categoryId       = '".$_POST['categoryId']."',
                             subCategoryId    = '".$_POST['subCategoryId']."',
                             itemMake         = '".$_POST['itemMake']."',
                             modelNo          = '".$_POST['modelNo']."',
                             currentStock     = '".$_POST['currentStock']."',
                             maxLevel         = '".$_POST['maxLevel']."',
                             minLevel         = '".$_POST['minLevel']."'
                      WHERE itemId            = '".$_REQUEST['itemId']."'";
      $updateItemRes  = mysql_Query($updateItem);
      if(!$updateItemRes)
        die("Update Query Not Inserted : ".mysql_error());
      else
        header("Location:item.php");
    }
    else
    {
      $itemMake     = isset($_REQUEST['itemMake']) ? $_REQUEST['itemMake'] : "";
      $modelNo      = isset($_REQUEST['modelNo']) ? $_REQUEST['modelNo'] : "";
      $currentStock = (isset($_REQUEST['currentStock']) && $_REQUEST['currentStock'] != "") ? $_REQUEST['currentStock'] : 0;
      $maxLevel     = (isset($_REQUEST['maxLevel']) && $_REQUEST['maxLevel'] != "") ? $_REQUEST['maxLevel'] : 0;
      $minLevel     = (isset($_REQUEST['minLevel']) && $_REQUEST['minLevel'] != "") ? $_REQUEST['minLevel'] : 0;
      
      $insertItem = "INSERT INTO item (categoryId,subCategoryId,itemName,itemMake,modelNo,currentStock,maxLevel,minLevel)
                      VALUE (".$_POST['categoryId'].",".$_POST['subCategoryId'].",'".$_POST['itemName']."','".$itemMake."','".$itemMake."',
                             ".$currentStock.",".$maxLevel.",".$minLevel.")";
      $insertItemResult = mysql_query($insertItem);
      if(!$insertItemResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:item.php");
    }
  }    
  // after post : end
  
  $selectItem = "SELECT item.itemId,item.categoryId,item.subCategoryId,item.itemName,item.itemMake,item.modelNo,item.currentStock,item.maxLevel,item.minLevel,
                        category.categoryId,category.categoryName,subcategory.subCategoryId,subcategory.subCategoryName
                   FROM item
                   JOIN category
                   JOIN subcategory
                  WHERE item.categoryId = category.categoryId
                    AND item.subCategoryId = subcategory.subCategoryId
               ORDER BY item.itemName";
  $selectItemResult = mysql_query($selectItem);
  $itemRow = mysql_num_rows($selectItemResult);
  while($itemRow = mysql_fetch_array($selectItemResult))
  {
    if($itemRow['itemId'] == $itemId)
    {
      $isEdit           = 1;  
      $itemName         = htmlspecialchars($itemRow['itemName'], ENT_QUOTES);  
      $itemMake         = $itemRow['itemMake'];  
      $modelNo          = $itemRow['modelNo'];  
      $categoryId       = $itemRow['categoryId'];  
      $categoryName     = $itemRow['categoryName'];  
      $subCategoryId    = $itemRow['subCategoryId'];  
      $subCategoryName  = $itemRow['subCategoryName'];  
      $currentStock     = $itemRow['currentStock'];  
      $maxLevel         = $itemRow['maxLevel'];  
      $minLevel         = $itemRow['minLevel'];  
    }
    $i++;     
  }    

	////Select category : Start
	$selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                      ORDER BY categoryName";
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	$j = 0;
	while($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  if($j == 0 || (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] == $categoryRow['categoryId']))
	    $categorySelected = $categoryRow['categoryId'];
	
	  $categoryIdArr[$j]     = $categoryRow['categoryId'];
	  $categoryNameArr[$j]   = $categoryRow['categoryName'];
	  $j++;
	}   
	////Select category : End
	
	////Select Subcategory : Start
	$selectSubCategoryQuery = "SELECT subCategoryId,subCategoryName
	                             FROM subcategory
	                         ORDER BY subCategoryName";
	$selectSubCategoryQueryRes = mysql_query($selectSubCategoryQuery);
	$i = 0;
	while($subCategoryRow = mysql_fetch_array($selectSubCategoryQueryRes))
	{
	  if($i == 0 || (isset($_REQUEST['subCategoryId']) && $_REQUEST['subCategoryId'] == $subCategoryRow['subCategoryId']))
	  $subCategorySelected    = $subCategoryRow['subCategoryId'];
	  $subCategoryIdArr[$j]   = $subCategoryRow['subCategoryId'];
	  $subCategoryNameArr[$j] = $subCategoryRow['subCategoryName'];
	  $j++;
	}   
	////Select Subcategory : End
	
	////Select Subcategory : Start
  $selectItemQuery = "SELECT itemId,itemName
	                             FROM item
	                         ORDER BY itemName";
	$selectItemQueryRes = mysql_query($selectItemQuery);
	$b = 0;
	while($itemRow = mysql_fetch_array($selectItemQueryRes))
	{
	  if($b == 0 || (isset($_REQUEST['itemId']) && $_REQUEST['itemId'] == $subCategoryRow['itemId']))
	  $itemSelected    = $itemRow['itemId'];
	  $itemIdArr[$b]   = $itemRow['itemId'];
	  $itemNameArr[$b] = $itemRow['itemName'];
	  $b++;
	}   
	////Select Subcategory : End
  include("./bottom.php");
  $smarty->assign("itemId",$itemId);
  $smarty->assign("itemName",$itemName);
  $smarty->assign("itemMake",$itemMake);
  $smarty->assign("modelNo",$modelNo);
  $smarty->assign("itemDetailArray",$itemDetailArray);
  $smarty->assign("i",$i);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("categoryIdArr",$categoryIdArr);
  $smarty->assign("categoryNameArr",$categoryNameArr);
  $smarty->assign("subCategoryIdArr",$subCategoryIdArr);
  $smarty->assign("subCategoryNameArr",$subCategoryNameArr);
  $smarty->assign("itemSelected",$itemSelected);
  $smarty->assign("itemIdArr",$itemIdArr);
  $smarty->assign("itemNameArr",$itemNameArr);
  $smarty->assign("editDetailArray",$editDetailArray);
  $smarty->assign("isEdit",$isEdit);
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryName",$categoryName);
  $smarty->assign("subCategoryName",$subCategoryName);
  $smarty->assign("subCategoryId",$subCategoryId);
  $smarty->assign("currentStock",$currentStock);
  $smarty->assign("maxLevel",$maxLevel);
  $smarty->assign("minLevel",$minLevel);
  $smarty->display("item.tpl");
}
?>